package com.hongning.railway.ui.login;

import android.app.Application;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.hongning.railway.app.Constant;
import com.hongning.railway.entity.LoginEntity;
import com.hongning.railway.entity.reqbean.ReqAddScoreBean;
import com.hongning.railway.ui.main.MainActivity;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.util.GsonUtil;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Author: zhangjing
 * DATA:2019-07-29 16:31
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class LoginViewModel extends BaseViewModel<AppRepository> {

    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");
    //用户名清除按钮的显示隐藏绑定
    public ObservableInt clearBtnVisibility = new ObservableInt();
    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<String> pString=new SingleLiveEvent<>();
    }

    public LoginViewModel(@NonNull Application application, AppRepository repository) {
        super(application, repository);
        //从本地取得数据绑定到View层
        if (model.getUserName()!=null)
        {
            userName.set(model.getUserName());
        }

        if (model.getPassword()!=null&&!TextUtils.isEmpty(model.getPassword()))
        {
            password.set(model.getPassword());
        }

    }

    //登录按钮的点击事件
    public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            login();
        }
    });

    /**
     * 网络模拟一个登陆操作
     **/
    private void login() {
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }
        //RaJava模拟登录
        model.login(userName.get(),password.get())
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).doOnTerminate(new Action() {
                    @Override
                    public void run() throws Exception {
                        dismissDialog();
                    }
                }).subscribe(new ApiDisposableObserver<LoginEntity>() {
                    @Override
                    public void onResult(LoginEntity response) {
                        model.saveUserName(response.getUserName());
                        //进入DemoActivity页面
                        SPUtils.getInstance().put(Constant.user_password, password.get());
                        SPUtils.getInstance().put(Constant.userinfo, GsonUtil.GsonString(response));
                        startActivity(MainActivity.class);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                ReqAddScoreBean reqAddScoreBean = new ReqAddScoreBean();
                                reqAddScoreBean.setProjectId("001001001001");
                                Messenger.getDefault().send(reqAddScoreBean,Constant.ADD_SCORE);
                            }
                        },1000);
                        //关闭页面
                        finish();
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
